package com.atcjx.service;

import com.atcjx.MyUtils.IdUtils;
import com.atcjx.MyUtils.wordSegUtils;
import com.atcjx.mapper.ArticleMapper;
import com.atcjx.mapper.FileMapper;
import com.atcjx.mapper.TagMapper;
import com.atcjx.mapper.wordMapper;
import com.atcjx.pojo.Article.Article;
import com.atcjx.pojo.Article.ArticleDTO;
import com.atcjx.pojo.Article.ArticleVO;
import com.atcjx.pojo.Article.baseArticle;
import com.atcjx.pojo.Query.Article.ArticleQueryDTO;
import com.atcjx.pojo.Query.Article.ArticleQueryPo;
import com.atcjx.pojo.keyWord;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName ArticleServiceImpl.java
 * @Description TODO
 * @createTime 2021年09月20日 15:41:00
 */
@Slf4j
@Service
public class ArticleServiceImpl {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    wordMapper keyWordMapper;
    @Autowired
    TagMapper tagMapper;

    @Autowired
    FileMapper fileMapper;

    public PageInfo<ArticleVO> getArticles(ArticleQueryDTO articleQueryDTO, int page, int limit) {
        ArticleQueryPo articleQueryPo = articleQueryDTO.toArticleQueryPo();
        PageHelper.startPage(page, limit);
        List<Article> articles = articleMapper.getArticlesByCondition(articleQueryPo);
        PageInfo pageInfo = new PageInfo(articles);
        ArrayList<ArticleVO> articleVOS = Lists.newArrayList();
        for (Article article : articles) {
            articleVOS.add(ArticleVO.fromArticlePO(article));
        }
        pageInfo.setList(articleVOS);
        return pageInfo;
    }

    @Async("taskExecutor")
    public String insArticle(ArticleDTO articleDTO) {
        long begin = System.currentTimeMillis();
        Article article = articleDTO.toArticlePo(false);
        String id = IdUtils.getRandomStringId();
        article.setId(id);
        articleMapper.insert(article);
        insertKeyword(articleMapper.getArticlesByCondition(new ArticleQueryPo(article.getId())).get(0));
        tagMapper.deleteTagArticleMapping(article.getId());
        for (String tags : article.getArtTags()) {
            String articleId = article.getId();
            tagMapper.addArticleMapping(articleId, tags);
        }
        long end = System.currentTimeMillis();
        log.info("文章《{}》已经上传完成，上传耗时为:{} s",article.getTitle(),(end-begin));
        return id;
    }

    @Async("taskExecutor")
    public void deleteById(String id) {
        keyWordMapper.deleteKeywordById(id);
        articleMapper.deleteArticle(id);
    }

    @Async("taskExecutor")
    public void updateArticle(ArticleDTO articleDTO, String id) {
        long begin=System.currentTimeMillis();
        List<Article> articles = articleMapper.getArticlesByCondition(new ArticleQueryPo(id));
        List<keyWord> list = wordSegUtils.segmentArticle(articles.get(0));
        keyWordMapper.deleteKeywordById(id);
        for (keyWord keyWord : list) {
            keyWordMapper.insertKeyword(keyWord.getWord(), id, keyWord.getPriority());
        }
        Article article = articleDTO.toArticlePo(true);
        article.setId(id);
        articleMapper.updateById(article);
        tagMapper.deleteTagArticleMapping(id);
        for (String tags : article.getArtTags()) {
            tagMapper.addArticleMapping(id, tags);
        }
        long end = System.currentTimeMillis();
        log.info("文章《{}》更新完成，用时为:{}ms",article.getTitle() ,(end - begin));
    }

    @Async("taskExecutor")
    public void updateAllArticle() {
        long begin = System.currentTimeMillis();
        List<Article> articlesByCondition = articleMapper.getArticlesByCondition(null);
        for (Article article : articlesByCondition) {
            insertKeyword(article);
        }
        long end=System.currentTimeMillis();
        log.info("文库关键字更新完毕！总用时为：{}s", (end - begin) / 1000);
    }

    @Async("taskExecutor")
    public void insertKeyword(baseArticle article){
        keyWordMapper.deleteKeywordById(article.getId());
        List<keyWord> keyWords = wordSegUtils.segmentArticle(article);
        for (keyWord keyWord : keyWords) {
            keyWordMapper.insertKeyword(keyWord.getWord(), article.getId(), keyWord.getPriority());
        }
    }
    private String preUrl = "https://gitee.com/Cjx_723/my-pic-warehouse/raw/master";
    private String url = "https://pic-1307682369.cos.ap-beijing.myqcloud.com/picGo";
    public void updateMd() {
        List<String> articlesByCondition = fileMapper.getPic();
        for (String picture : articlesByCondition) {
            if(!StringUtils.isEmpty(picture))
            fileMapper.updateImg(picture, picture.replace(preUrl, url));
        }
    }
}
